import util from '../../../util/index.js'
let isEnd = false;
let curPage = 0;
let source = [];
let rid;
let stations;
/**
 * 司机查看的预约记录
 */
Page({

  /**
   * 页面的初始数据
   */
  data: {
    data: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function({
    id,
    number
  }) {
    wx.setNavigationBarTitle({
      title: number + '预约记录',
    })
    rid = id;
    stations = [];
    util.ajax("route", {
      data: {
        action: 'detail',
        id
      }
    }).then(({
      code,
      data
    })=>{
      if(code == 1){
        stations = data.stations;
      }
      this.getData(1);
    });
    

  },
  call({
    currentTarget: {
      dataset: {
        phone
      }
    }
  }) {
    wx.makePhoneCall({
      phoneNumber: phone,
    })
  },
  getData(page) {
    if(page == 1){
      isEnd = false;
      source = [];
    }
    if(isEnd){
      return;
    }
    curPage = page;
    wx.showNavigationBarLoading();
    util.ajax("order", {
      data: {
        action: "list",
        rid,
        page,
      }
    }).then(({
      code,
      data,
      msg
    }) => {
      wx.hideNavigationBarLoading();
      if (code == 1) {
        if (data.length == 0) {
          isEnd = true;
          util.tip("暂无数据")
        }
        if(page != 1){
          data = [...source,...data]
        }
        source = data;
        let result = {};
        data.map(item => {
          if (result[item.date]) {
            result[item.date].push(item);
          } else {
            result[item.date] = [item];
          }
        })
        Object.keys(result).map(item=>{
          result[item] = this.sort(result[item])
        })
        this.setData({
          data: Object.values(result)
        })
      } else {
        util.tip(msg);
      }
    }, wx.hideNavigationBarLoading)
  },
  onReachBottom(){
    this.getData(++curPage);
  },
  sort(list){
    let result = [];
    stations.map(item=>{
      list.map(i=>{
        if(item.name == i.rName){
          result.push(i);
        }
      })
    })
    return result;
  }
})